package com.kexinxin.util.search;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.spell.LevensteinDistance;
import org.apache.lucene.search.spell.LuceneDictionary;
import org.apache.lucene.search.spell.SpellChecker;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

public class CheckWordUtil {
	private static Directory skillDirectory = null;
	private static Directory checkWordDirectory = new RAMDirectory();
	public static String getSuggesion(String str) throws Exception {
		String result = null;
		if(skillDirectory==null){
			skillDirectory = StoreFileUtils.getSkillDirectory();
		}
		
		Directory skillDirectory = StoreFileUtils.getSkillDirectory();
		Directory checkWordDirectory = new RAMDirectory();

		SpellChecker spell = new SpellChecker(checkWordDirectory); // #A
		IndexReader r = IndexReader.open(skillDirectory);
		spell.indexDictionary(new LuceneDictionary(r, "skillName"));

		spell.setStringDistance(new LevensteinDistance()); // #B

		String wordToRespell = str;
		String[] suggestions = spell.suggestSimilar(wordToRespell, 5); // #C
		if (suggestions.length > 0) {
			result = suggestions[0];
		}

		return result;
	}
}
